package js.dwim.client.dialogs;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.UIManager;

import js.dwim.client.CommunicationException;
import js.dwim.client.Application;
import java.awt.Color;

public class LoginDialog
        extends JDialog
{
    private static final long serialVersionUID = 1L;
    
    private JTextField txtUser;
    private JPasswordField pwdPassword;
    private JTextArea txtError;

    /**
     * Create the frame.
     */
    public LoginDialog()
    {
        setTitle("Login");
        setDefaultCloseOperation( JDialog.HIDE_ON_CLOSE );
        setBounds( 100, 100, 400, 230 );
        getContentPane().setLayout(null);
        
        JButton btnLogin = new JButton("Login");
        btnLogin.addActionListener(new ActionListener() {
            
            @SuppressWarnings( "deprecation" )
            public void actionPerformed(ActionEvent arg0) 
            {
                try
                {
                    Application.getState().connect( txtUser.getText(), pwdPassword.getText() );
                    txtError.setText( "" );
                    setVisible( false );
                }
                catch ( CommunicationException e )
                {
                    txtError.setText( e.getMessage() );
                    txtError.repaint();
                }
                catch ( InterruptedException e )
                {
                    txtError.setText( e.getMessage() );
                    txtError.repaint();
                }
            }
        });
        btnLogin.setBounds(273, 86, 107, 25);
        getContentPane().add(btnLogin);
        
        JLabel lblUsername = new JLabel("Username:");
        lblUsername.setBounds(12, 12, 117, 15);
        getContentPane().add(lblUsername);
        
        JLabel lblPassword = new JLabel("Password:");
        lblPassword.setBounds(12, 47, 117, 15);
        getContentPane().add(lblPassword);
        
        txtUser = new JTextField();
        txtUser.setText("jsaveljevs");
        txtUser.setBounds(147, 10, 233, 19);
        getContentPane().add(txtUser);
        txtUser.setColumns(10);
        
        pwdPassword = new JPasswordField();
        pwdPassword.setBounds(147, 45, 233, 19);
        getContentPane().add(pwdPassword);
        
        txtError = new JTextArea();
        txtError.setWrapStyleWord(true);
        txtError.setForeground(Color.RED);
        txtError.setLineWrap(true);
        txtError.setBackground(UIManager.getColor("Panel.background"));
        txtError.setBounds(12, 134, 368, 57);
        getContentPane().add(txtError);
    }
}
